﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

namespace Agresso.Interface.Layer.Service 
{
    public class LogService : IService
    {
        public static void WriteLog(string log)
        {
            bool isLog = Convert.ToBoolean(ConfigurationManager.AppSettings["isLog"]);
            if (isLog)
            {
                string m_logPath = ConfigurationManager.AppSettings["logPath"];
                string fileName = m_logPath + DateTime.Today.ToString("ddMMyyyy") + "_AgressoInterfaceLayerLog.txt";
                if (!File.Exists(fileName))
                {
                    FileStream fs = File.Create(fileName);
                    fs.Close();
                }
                StreamWriter sw = File.AppendText(fileName);
                sw.WriteLine(DateTime.Now.ToString() + " - " + log);
                sw.Close();
            }
        }
        public static void WriteLogExecuteSQL(string sql)
        {
            WriteLog("Execute SQL :" + sql);
        }
        public static void WriteLogExecuteSQLCommand(SqlCommand sql)
        {
            WriteLog("Execute SQL [" + sql.Connection.Database +"] : "+ sql.CommandText);
        }
        public static void WriteLogException(Exception exception)
        {
           // WriteLog("Exception [" + exception.GetType().ToString() + "] : " + exception.Message);
            while (exception != null)
            {
                WriteLog("Exception [" + exception.GetType().ToString() + "] : " + exception.Message);
                exception = exception.InnerException;
            }
         //   if (exception.InnerException != null)
           //     WriteLogException(exception.InnerException);
        }
    }
}
